/**
 * Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#import "CabDriver.h"

#import "ServerComm.h"
#import "SBJSON.h"

@implementation CabDriver

@synthesize dict;

+ (NSArray*)allCabDrivers
{
	NSMutableArray* cabArray = [[NSMutableArray alloc] init];
	SBJSON* json = [[SBJSON alloc] init];
	NSString* str = [ServerComm sendRequestWithURL:HOSTNAME"cabs" method:@"GET" body:@""];
	NSArray* jsonCabArray = [json objectWithString:str];
	
	for(NSDictionary* jsonCab in jsonCabArray)
	{
		[cabArray addObject:[CabDriver CabDriverWithDict:jsonCab]];
	}
	return [cabArray retain];
}

+ (NSArray*)allUnassignedCabDrivers
{
	NSArray* cabArray = [CabDriver allCabDrivers];
	NSArray* passengerArray = [Passenger allPassengers];
	
	NSMutableArray* unassignedArray = [[NSMutableArray alloc] init];
	
	for(CabDriver* cab in cabArray)
	{
		BOOL assigned = NO;
		for(Passenger* p in passengerArray)
		{
			if([[p cabid] intValue] == [cab ID])
			{
				assigned = YES;
				break;
			}
		}
		if(!assigned)
			[unassignedArray addObject:cab];
	}
	return unassignedArray;
}

+ (CabDriver*)CabDriverWithDict:(NSDictionary*)dictionary
{
	CabDriver* p = [[CabDriver alloc] init];
	p.dict = [dictionary retain];
	return p;
}

+ (CabDriver*)CabDriverWithID:(NSInteger)ID
{
	SBJSON* json = [[SBJSON alloc] init];
	NSString* str = [ServerComm sendRequestWithURL:[NSString stringWithFormat:HOSTNAME"cabs/%d",ID] method:@"GET" body:@""];
	return [CabDriver CabDriverWithDict:[json objectWithString:str]];
}

+ (void)deleteAll
{
	[ServerComm sendRequestWithURL:HOSTNAME"cabs" method:@"DELETE" body:@""];
}


- (id)init
{
	if(self = [super init])
	{
		self.dict = [[NSMutableDictionary alloc] init];
	}
	return self;
}

- (NSString*)description
{
	return [dict description];
}

- (NSInteger)count
{
	return [dict count];
}

- (void)setObject:(NSObject*)object forKey:(NSString*)key
{
	[dict setObject:object forKey:key];
}

- (id)objectForKey:(NSString*)key
{
	return [dict objectForKey:key];
}

- (NSInteger)ID
{
	NSLog(@"Dict = %@",dict);
	return [[dict objectForKey:@"id"] integerValue];
}

- (NSString*)latitude
{
	return [dict objectForKey:@"latitude"];
}

- (NSString*)longitude
{
	return [dict objectForKey:@"longitude"];
}

- (void)setLatitude:(NSString*)latitude
{
	[dict setObject:latitude forKey:@"latitude"];
}

- (void)setLongitude:(NSString*)longitude
{
	[dict setObject:longitude forKey:@"longitude"];
}


- (void)save
{
	SBJSON* json = [[SBJSON alloc] init];
	NSString* jsonObj = [json stringWithObject:dict];
	NSLog(@"Jsonstr = %@",jsonObj);
	NSString* str;
	if([self ID] == 0)
		str = [ServerComm sendRequestWithURL:HOSTNAME"cabs" method:@"PUT" body:jsonObj];
	else
		str = [ServerComm sendRequestWithURL:[NSString stringWithFormat:HOSTNAME"cabs/%d",[self ID]] method:@"POST" body:jsonObj];
	
	self.dict = [json objectWithString:str];
}

- (NSString*)name
{
	return [dict objectForKey:@"name"];
}

- (void)setName:(NSString*)name
{
	[dict setObject:name forKey:@"name"];
}


@end
